# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : rig_editor.py
# Date      : 2019/12/6 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import traceback

import maya.cmds as mc

import editors.editor as editor
reload(editor)
import scripts.commonCmds as con
import rig.twoCurve as twoCurve
import rig.sample as sample
import rig.rpIkhandle as rpIkhandle
import rig.spIkhandle as spIkhandle

class RigEditor(editor.Editor):
    def __init__(self):
        super(RigEditor,self).__init__()
        self.commonRig_tf = "commonRig_tf"
    def win(self):

        # self.isExist("sss")
        # mc.workspaceControl("sss", retain=False, floating=True,
        #                     uiScript=lambda *args: self.selfWindow())
        _win = self.selfWorkspaceControl()
        # mc.window(_win,e=True,dc=["topRight","right"])
        self.main_layout()

        # mc.showWindow(_win)

        return
    
    
    def main_layout(self):
        form = mc.formLayout()
        column = mc.columnLayout(adj=True)
        mc.text(l="Rig Toolbox",h=20)
        mc.separator()
        mc.textFieldGrp(self.commonRig_tf,l="**kwargs : ",tx="L_")
        mc.separator()
        self.frame_commonRig()
        self.frame_tool()
        self.frame_ikRig()
        mc.setParent("..")
        copyright = self.copyright()
        
        mc.setParent("..")

        mc.formLayout(form, edit=True,
                        attachForm=[(column, 'top', 0), (column, 'left', 0), (column, 'right', 0),
                                    (copyright, 'bottom', 3), (copyright, 'left', 0), (copyright, 'right', 0)],
                        attachControl=[(column, 'bottom', 5, copyright)],
                        # attachPosition=[(column, 'top', 5, 75), (column, 'left', 0, 75)]
                      )
        
        return
    def frame_commonRig(self,**kwargs):
        mc.frameLayout("commonRig",cl=False,cll=True)
        mc.columnLayout(adj=True)

        
        row = mc.rowLayout(nc=9)
        mc.checkBox("selectToOneControl_cb",l="Mirror",v=True)
        mc.button(l="Select To One Control",c=lambda *args:self.btn_selectToOneControl())
        mc.setParent("..")
        
        row = mc.rowLayout(nc=9)
        mc.checkBox("selectJointChainToFk_cb",l="Mirror",v=True)
        mc.button(l="Select Joint Chain To Fk",c=lambda *args:self.btn_selectJointChainToFk())
        mc.setParent("..")
        
        row = mc.rowLayout(nc=9)
        mc.text(l="Select Two Curve To Rig : ")
        mc.intField("twoCurveRig_if",v=9)
        mc.button(l="Create",c=lambda *args:self.btn_twoCurveRig())
        mc.setParent("..")
        
        mc.setParent("..")
        mc.setParent("..")
        
        return
    def btn_twoCurveRig(self,):
        reload(twoCurve)
        sel = mc.ls(sl=True)
        if not sel and len(sel) != 2:
            return
        tf = mc.textFieldGrp(self.commonRig_tf,q=True,tx=True)
        num = mc.intField("twoCurveRig_if",q=True,v=True)

        tc = twoCurve.TwoCurve(tf)
        tc.create(sel[0], sel[1], num)
        
        return
    
    def btn_selectToOneControl(self):
        reload(sample)
        tf = mc.textFieldGrp(self.commonRig_tf,q=True,tx=True)
        mb = mc.checkBox("selectToOneControl_cb",q=True,v=True)
        
        sample.selectTo(tf,mirror=mb)
        return
    
    def btn_selectJointChainToFk(self):
        reload(sample)
        tf = mc.textFieldGrp(self.commonRig_tf,q=True,tx=True)
        mb = mc.checkBox("selectJointChainToFk_cb",q=True,v=True)
        sel = mc.ls(sl=True)
        sample.selectJointChain(sel,tf,mirror=mb)

        return
    
    def frame_tool(self):
        mc.frameLayout("Tool",cl=False,cll=True)
        mc.gridLayout(numberOfColumns=2, cellWidthHeight=(60, 60))
        mc.button(l="make center crv")
        mc.setParent("..")
        mc.separator()
        mc.setParent("..")
        return
    def frame_ikRig(self):
        mc.frameLayout("ikRig",cl=False,cll=True)
        rp_frame = mc.frameLayout(" - RpIkhandle",cl=False,cll=True)
        self.lay_rpIkhandle(rp_frame)
        mc.setParent("..")
        
        rp_frame = mc.frameLayout(" - SpIkhandle",cl=False,cll=True)
        self.lay_spIkhandle(rp_frame)
        mc.setParent("..")
        
        mc.setParent("..")

        return
    
    def lay_rpIkhandle(self,parent_layout):
        mc.columnLayout(parent=parent_layout,adj=True)
        tfbg1 = "rp_starJoint_tfbg"
        mc.textFieldButtonGrp(tfbg1,l="Star Joint : ",bl="Load",cw=[1,100],ed=False,
                              bc=lambda *args:self.btn_load(tfbg1))
        tfbg2 = "rp_endJoint_tfbg"
        mc.textFieldButtonGrp(tfbg2,l="End Joint : ",bl="Load",cw=[1,100],ed=False,
                              bc=lambda *args:self.btn_load(tfbg2))
        tfbg3 = "rp_polarObject_tfbg"
        mc.textFieldButtonGrp(tfbg3,l="* Polar Object : ", bl="Load",cw=[1,100],ed=False,
                              bc=lambda *args:self.btn_load(tfbg3))
        mc.separator()
        row = mc.rowLayout(nc=9)
        stretch_cb = "rp_stretch_cb"
        mc.checkBox(stretch_cb,l=" Streth",v=True)
        lockPolar_cb = "rp_lockPolar_cb"
        mc.checkBox(lockPolar_cb,l=" Lock Polar",v=True)
        mc.setParent("..")
        
        mc.separator()
        mc.button("Create",c=lambda *args:self.btn_rpIkhandle(tfbg1,
                                                              tfbg2,
                                                              tfbg3,
                                                              stretch_cb,
                                                              lockPolar_cb,
                                                              ))
        mc.setParent("..")
        return
    def lay_spIkhandle(self,parent_layout):
        mc.columnLayout(parent=parent_layout,adj=True)
        
        tabs = mc.tabLayout()
        jotTo_ = mc.columnLayout(adj=True)

        tfbg1 = "sp_starJoint_tfbg"
        mc.textFieldButtonGrp(tfbg1,l="Star Joint : ",bl="Load",cw=[1,100],ed=False,
                              bc=lambda *args:self.btn_load(tfbg1))
        tfbg2 = "sp_endJoint_tfbg"
        mc.textFieldButtonGrp(tfbg2,l="End Joint : ",bl="Load",cw=[1,100],ed=False,
                              bc=lambda *args:self.btn_load(tfbg2))
        tfbg3 = "sp_curve_tfbg"
        mc.textFieldButtonGrp(tfbg3,l="* Curve Object : ", bl="Load",cw=[1,100],ed=False,
                              bc=lambda *args:self.btn_load(tfbg3))
        mc.separator()
        row = mc.rowLayout(nc=9)
        stretch_cb = "sp_stretch_cb"
        mc.checkBox(stretch_cb,l="Stretch",v=True)
        advRotate_cb = "advRotate_cb"
        mc.checkBox(advRotate_cb,l="Adv Rotate",v=True)
        keepVolume_cb = "keepVolume_cb"
        mc.checkBox(keepVolume_cb,l="Keep Volume",v=True)
        mc.setParent("..")
        mc.separator()
        mc.button(l="Create",c=lambda *args:self.btn_spIkhandle(tfbg1,
                                                                tfbg2,
                                                                tfbg3,
                                                                stretch_cb,
                                                                advRotate_cb,
                                                                keepVolume_cb))
        mc.setParent("..")
        
        tc_lay = mc.columnLayout(adj=True)
        tc_tfbg1 = "tc_starCurve_tfbg"
        mc.textFieldButtonGrp(tfbg1, l="Curve : ", bl="Load", cw=[1, 100], ed=False,
                              bc=lambda *args: self.btn_load(tfbg1))
        
        
        mc.button(l="Create")
        mc.setParent("..")
        
        mc.setParent("..")

        mc.setParent("..")
        
        mc.tabLayout(tabs, edit=True,sti=2,
                     tabLabel=((jotTo_, 'Sp Ikhandle'),
                               (tc_lay,"Tc Ikhandle") ))

        return
    
    
    def btn_load(self,tfbg):
        sel = mc.ls(sl=True)
        if not sel:
            return
        mc.textFieldButtonGrp(tfbg,e=True,tx=sel[0])
        return
    
    def btn_rpIkhandle(self,sj_tfbg,ej_tfbg,po_tfbg,s_cb,lp_cb):
        
        sjot = mc.textFieldButtonGrp(sj_tfbg,q=True,tx=True)
        ejot = mc.textFieldButtonGrp(ej_tfbg,q=True,tx=True)
        if not sjot or not ejot:
            return
        po = mc.textFieldButtonGrp(po_tfbg,q=True,tx=True)
        
        stretch = mc.checkBox(s_cb,q=True,v=True)
        lp = mc.checkBox(lp_cb,q=True,v=True)
        
        kworld = mc.textFieldGrp(self.commonRig_tf,q=True,tx=True)
        
        reload(rpIkhandle)
        ri = rpIkhandle.RpIkhandle(lname=kworld)
        ri.create(sjot,ejot,po)
        if stretch:
            ri.stretch()
        if lp:
            ri.lockPolar()
        return
    
    def btn_spIkhandle(self,sj_tfbg,ej_tfbg,crv_tfbg,s_cb,ar_cb,kv_cb):
        sjot = mc.textFieldButtonGrp(sj_tfbg,q=True,tx=True)
        ejot = mc.textFieldButtonGrp(ej_tfbg,q=True,tx=True)
        if not sjot or not ejot:
            return
        crv = mc.textFieldButtonGrp(crv_tfbg,q=True,tx=True)
        kworld = mc.textFieldGrp(self.commonRig_tf,q=True,tx=True)
        stretch = mc.checkBox(s_cb,q=True,v=True)
        advRotate = mc.checkBox(ar_cb,q=True,v=True)
        keepVolumn = mc.checkBox(kv_cb,q=True,v=True)

        reload(spIkhandle)
        
        sp = spIkhandle.SpIkhandle(lname=kworld)
        sp.create(sjot,ejot,crv)
        if stretch:
            sp.stretch()
        if advRotate:
            sp.advRotate()
        if keepVolumn:
            sp.keepVolumn()
        return
    
def showEditor():
    RigEditor().win()

if __name__ == '__main__':
    pass